package fr.insta.browser.view.frame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import fr.insta.browser.view.menu.IJMenuBarWebView;
import fr.insta.webview.object.IWebObject;

public class HistoryPanel extends JPanel
{
	private static final long serialVersionUID = 6000321193195335815L;
	
	private ToolsFrame toolsFrame;
	
	public HistoryPanel(ToolsFrame _toolsFrame)
	{
		toolsFrame = _toolsFrame;
		this.setLayout(new BorderLayout());
		
		final JLabel historyInfoLabel = new JLabel(getWelcomeMessage());
		historyInfoLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(historyInfoLabel, BorderLayout.NORTH);
		
		JPanel panelCentreCascade = new JPanel();
		panelCentreCascade.setLayout(new BoxLayout(panelCentreCascade, BoxLayout.PAGE_AXIS));
		
		this.add(panelCentreCascade, BorderLayout.CENTER);
		
		JButton buttonGotoHistory = new JButton("Voir l'historique");
		buttonGotoHistory.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent _arg0)
			{
				IJMenuBarWebView barWebView = toolsFrame.getParentWindow().getJMenuBarWebView();
				List<IWebObject> webOBject = barWebView.getListHistory();
				
				new WebObjectFrame(barWebView, webOBject);
			}
		});
		
		final JButton buttonSwitchNavigationMethod = new JButton(getButtonChangeHistoryMessage());
		buttonSwitchNavigationMethod.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent _arg0)
			{
				toolsFrame.getParentWindow().changeHistoryState();
				buttonSwitchNavigationMethod.setText(getButtonChangeHistoryMessage());
				historyInfoLabel.setText(getWelcomeMessage());
			}
		});
		
		panelCentreCascade.add(buttonGotoHistory);
		panelCentreCascade.add(buttonSwitchNavigationMethod);
	}
	
	private int getHistoryState() {
		return toolsFrame.getParentWindow().getHistoryState();
	}
	
	private String getWelcomeMessage() {
		String returnMessage = "Vous êtes actuellement en : " + 
				(getHistoryState() == MainFrame.HISTORY_PUBLIC ? 
						"navigation publique" : "navigation privée");
		
		return returnMessage;
	}
	
	private String getButtonChangeHistoryMessage() {
		String returnMessage = "Passer en : " + 
				(getHistoryState() == MainFrame.HISTORY_PUBLIC ? 
						"navigation privée" : "navigation publique");
		
		return returnMessage;
	}
	
}